package com.cmhi.softmbh.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;

import com.cmhi.softmbh.cache.StaticConst;
import com.softmbh.kellyplatform.util.AppUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

/**
 * 对ZhongXing表进行操作的ContentProvider
 * @author jacp
 */
public class ZhongXingProvider extends ContentProvider {
    private static final int PROGRAMMERS = 1;
//    private static final int PROGRAMMERS_ID = 2;
    private static final UriMatcher sUriMatcher;
    private DatabaseHelper mOpenHelper;
    String TAG = "baimeng";

    @Override
    public boolean onCreate() {
         AppUtils.Log(TAG, "ZhongXingProvider.onCreate");
        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         AppUtils.Log(TAG, "ZhongXingProvider.query,isForeground:" + StaticConst.isForeground);
//        if (!StaticConst.isForeground)
//            return null;
         AppUtils.Log(TAG, Arrays.toString(selectionArgs));
        Cursor mCur = null;
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if (db != null && db.isOpen()) {
            if (Provider.ZhongXingColumns.TABLE_NAME.equals(uri.getLastPathSegment())) {
                mCur = db.query(Provider.ZhongXingColumns.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
            } else {
                mCur = db.query(Provider.ZhongXingColumns.TABLE_NAME, null, Provider.ZhongXingColumns.NAME +
                        " = '" + uri.getLastPathSegment() + "'", null, null, null, sortOrder);
            }
        }
        return mCur;
    }

    @Override
    public String getType(@NonNull Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case PROGRAMMERS:
                return Provider.CONTENT_TYPE;
//            case PROGRAMMERS_ID:
//                return Provider.CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(@NonNull Uri uri, ContentValues initialValues) {
         AppUtils.Log(TAG, "ZhongXingProvider.insert");
        // Validate the requested uri
        if (sUriMatcher.match(uri) != PROGRAMMERS) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }
        // Make sure that the fields are all set
        if (!values.containsKey(Provider.ZhongXingColumns.NAME)) {
            values.put(Provider.ZhongXingColumns.NAME, "");
        }
        if (!values.containsKey(Provider.ZhongXingColumns.VALUE)) {
            values.put(Provider.ZhongXingColumns.VALUE, "");
        }
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(Provider.ZhongXingColumns.TABLE_NAME, Provider.ZhongXingColumns.NAME, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(Provider.ZhongXingColumns.CONTENT_URI, rowId);
            Objects.requireNonNull(getContext()).getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(@NonNull Uri uri, String where, String[] whereArgs) {
         AppUtils.Log(TAG, "ZhongXingProvider.delete");
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case PROGRAMMERS:
                count = db.delete(Provider.ZhongXingColumns.TABLE_NAME, where, whereArgs);
                break;
//            case PROGRAMMERS_ID:
//                String noteId = uri.getPathSegments().get(1);
//                count = db.delete(Provider.ZhongXingColumns.TABLE_NAME, Provider.ZhongXingColumns._ID + "=" + noteId
//                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
//                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, ContentValues values, String where, String[] whereArgs) {
         AppUtils.Log(TAG, "ZhongXingProvider.update");
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case PROGRAMMERS:
                count = db.update(Provider.ZhongXingColumns.TABLE_NAME, values, where, whereArgs);
                break;
//            case PROGRAMMERS_ID:
//                String noteId = uri.getPathSegments().get(1);
//                count = db.update(Provider.ZhongXingColumns.TABLE_NAME, values, Provider.ZhongXingColumns._ID + "=" + noteId
//                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
//                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
        return count;
    }

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(Provider.ZhongXingColumns.AUTHORITY, "authentication", PROGRAMMERS);
//        sUriMatcher.addURI(Provider.ZhongXingColumns.AUTHORITY, "authentication/#", PROGRAMMERS_ID);

        HashMap<String, String> sprogrammersProjectionMap = new HashMap<>();
//        sprogrammersProjectionMap.put(Provider.ZhongXingColumns._ID, Provider.ZhongXingColumns._ID);
        sprogrammersProjectionMap.put(Provider.ZhongXingColumns.NAME, Provider.ZhongXingColumns.NAME);
        sprogrammersProjectionMap.put(Provider.ZhongXingColumns.VALUE, Provider.ZhongXingColumns.VALUE);
    }
}